package cn.com.homeyoda.aimate.aihealth.ui.health.viewModel;

import android.app.Application;

import androidx.annotation.NonNull;

import cn.com.homeyoda.aimate.aihealth.base.BaseViewModel;
import cn.com.homeyoda.aimate.aihealth.bean.SportTitleBean;
import cn.com.homeyoda.aimate.aihealth.data.Repository;
import com.wzq.mvvmsmart.event.StateLiveData;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

/**
 * Created By liuxinhua
 * Created Time 2025/2/27
 */
public class HealthModel extends BaseViewModel<Repository> {

    public StateLiveData<List<SportTitleBean>> stateLiveData;

    public HealthModel(@NonNull Application application, Repository model) {
        super(application, model);
        stateLiveData = new StateLiveData<>();
        stateLiveData.setValue(new ArrayList<SportTitleBean>());

    }

    public void sportTitleData(){
        Calendar calendar = Calendar.getInstance();

        // 设置一周的第一天为周一（默认是周日）
        calendar.setFirstDayOfWeek(Calendar.MONDAY);

        // 获取当前日期是本周的第几天（周一是1，周日是7）
        int currentDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

        // 调整到本周的第一天（周一）
        calendar.add(Calendar.DAY_OF_WEEK, -(currentDayOfWeek - Calendar.MONDAY));

        // 定义日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("d", Locale.getDefault());
        SimpleDateFormat dayFormat = new SimpleDateFormat("EEE", Locale.getDefault());

        List<SportTitleBean> sportTitleBeanList = new ArrayList<>();
        // 打印一周的日期和星期几
        for (int i = 0; i < 7; i++) {
            String dateStr = dateFormat.format(calendar.getTime());
            String weekStr = dayFormat.format(calendar.getTime());
            SportTitleBean sportTitleBean = new SportTitleBean();
            sportTitleBean.setDate(dateStr);
            sportTitleBean.setWeek(weekStr);
            sportTitleBeanList.add(sportTitleBean);
            // 增加一天
            calendar.add(Calendar.DAY_OF_WEEK, 1);
        }
        stateLiveData.postSuccess();
        stateLiveData.getValue().addAll(sportTitleBeanList);
        stateLiveData.postValueAndSuccess(stateLiveData.getValue());

    }

    public int getDatePos(){
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY); // 设置周一为第一天
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        int adjustedDayOfWeek = (dayOfWeek + 5) % 7;
        return adjustedDayOfWeek;
    }

}
